package com.placement.app.util;

import java.util.Enumeration;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This will be a static class which will have list of states
 * and abbreviation to validate states entered by the user
 * @author ns_nagar
 *
 */

public class AddressLookup {
	
	private static final Log logger = LogFactory.getLog(AddressLookup.class);
	private static ResourceBundle props=null;
	
	private String[] stateAbbr= {"AL","AK","AS","AZ","AR","CA","CO","CT","DE","DC",
										"FM","FL","GA","GU","HI","ID","IL","IN","IA","KS",
										"KY","LA","ME","MH","MD","MA","MI","MN","MS","MO",
										"MT","NE","NV","NH","NJ","NM","NY","NC","ND","MP",
										"OH","OK","OR","PW","PA","PR","RI","SC","SD","TN",
										"TX","UT","VT","VI","VA","WA","WV","WI","WY"};
		
	/**
	 * If this method returns string, it is a valid state
	 * @param state
	 * @return
	 */
	public String stateLookUp(String state){
			boolean validState = false;
			if (state != null && state.length()>0){
				if(state.length()==2){
					for (String str : stateAbbr) {
						if(state.equals(str)){
							validState = true;
							break;
						}
					}
					if(validState)
						return state;
				}
				else {
					
				}
			}
			return null;
		}

	public static String matchCity(String city) {
		// TODO - Add more logic to parse and try to find
		//		closed match
		// At this time this is just a cleanup process
		
		return city;
	}

	public static String matchState(String state) {
		// TODO Auto-generated method stub
		return state;
	}
	
	
	public static String getProperty(String propertyName)
	{
		String val = null;
		try
		{
			if ( props == null ){
				logger.debug("Started loading properties from statelist.properties ");
				props = ResourceBundle.getBundle("statelist.properties");
				if (logger.isDebugEnabled()) {
					
					Enumeration enum1=props.getKeys();
					String key=null;
					while(enum1.hasMoreElements())
					{
						key=(String)enum1.nextElement();
						logger.debug("KEY = "+key+" VALUE = "+(String)props.getObject(key));
					}
				}
				
			}
			
			//props = ResourceBundle.getBundle("srgt");
			
			val = props.getString(propertyName);
		}
		catch (Exception e)
		{
			logger.error(Constants.EVENT_TYPE_EXCEPTION + " "
					+ Constants.BUSINESS_PROCESS_NAME , e);

		}
		return val;
	}
}
